<?php 
	namespace Admin\Controller;

	class SortController extends CommonController
	{
		public function index()
		{
			$db = M('sort');
			if(I('post.')){
				$where['sort_name']=I('post.sort_name');
				$list = $db ->where($where)-> select();
			}else{
				$list = $db -> select();	
			}
			$this -> assign('list',$list);
			$this -> display();
			
		}

		public function add()
		{
			
				$db = M('sort');
				$sort_name = I('post.sort_name');
				if (!empty($sort_name)) 
				{
					$Model = new \Think\Model();
					$num = $Model->query("select count(*) as num from y_sort where sort_name=\"$sort_name\"");
					if ($num[0]['num'] === '0')  
					{
						$where['id'] = $id;
						$item['sort_name'] = $sort_name;
						$item['create_time'] = time();
						$item['update_time'] = time();
						$item['change_id'] = $_SESSION['admin_info']['aid'];
						$db -> where($where) -> save($item);
					}
					$db -> data($item) -> add();
					$this -> redirect('Sort/index'); 
				}
				else
				{
					$this -> redirect('Sort/index');
				}
			
		}

		public function edit()
		{
			$db = M('sort');
			$id = I('post.id');
			$sort_name = trim(I('post.sort_name'));
			if (!empty($sort_name)) 
			{
				$Model = new \Think\Model();
				$num = $Model->query("select count(*) as num from y_sort where sort_name=\"$sort_name\"");
				if ($num[0]['num'] === '0') 
				{
					$where['id'] = $id;
					$item['sort_name'] = $sort_name;
					$item['update_time'] = time();
					$item['change_id'] = $_SESSION['admin_info']['aid'];
					$db -> where($where) -> save($item);
				}
				$this -> redirect('Sort/index'); 
			}
			else
			{
				$this -> redirect('Sort/index');
			}
		}

		public function del()
		{
			$db = M('sort');
			$id = I('post.id');
			if (!empty($id)) 
			{
				$Model = new \Think\Model();
				$num = $Model->execute("delete from y_sort where id=\"$id\"");
				$this -> redirect('Sort/index'); 
			}
			else
			{
				$this -> redirect('Sort/index');
			}
		}
	}